package com.health.healthSys.service.impl;

import java.util.List;

import com.health.common.annotation.DataScope;
import com.health.common.core.domain.entity.SysUser;
import com.health.common.exception.ServiceException;
import com.health.healthSys.domain.HealthUsers;
import com.health.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.health.healthSys.mapper.HealthRecordsMapper;
import com.health.healthSys.domain.HealthRecords;
import com.health.healthSys.service.IHealthRecordsService;

/**
 * 健康数据Service业务层处理
 *
 * @author maple
 * @date 2025-02-28
 */
@Service
public class HealthRecordsServiceImpl implements IHealthRecordsService
{
    @Autowired
    private HealthRecordsMapper healthRecordsMapper;

    /**
     * 查询健康数据
     *
     * @param healthId 健康数据主键
     * @return 健康数据
     */
    @Override
    public HealthRecords selectHealthRecordsByHealthId(String healthId)
    {
        return healthRecordsMapper.selectHealthRecordsByHealthId(healthId);
    }

    /**
     * 查询健康数据列表
     *
     * @param healthRecords 健康数据
     * @return 健康数据
     */
    @Override
    @DataScope(userAlias = "hr")
    public List<HealthRecords> selectHealthRecordsList(HealthRecords healthRecords)
    {
        return healthRecordsMapper.selectHealthRecordsList(healthRecords);
    }

    /**
     * 新增健康数据
     *
     * @param healthRecords 健康数据
     * @return 结果
     */
    @Autowired
    private SysUserMapper sysUserMapper; // 替换为系统用户Mapper
    @Override
    public int insertHealthRecords(HealthRecords healthRecords) {
//        // 直接验证userId是否存在
//        HealthUsers user = healthUsersMapper.selectHealthUsersByUserid(healthRecords.getUserId());
//        if (user == null) {
//            throw new ServiceException("用户ID不存在");
//        }
//        // 自动填充userName（根据需求可选）
//        healthRecords.setUserName(user.getUsername());
        return healthRecordsMapper.insertHealthRecords(healthRecords);
    }

    /**
     * 修改健康数据
     *
     * @param healthRecords 健康数据
     * @return 结果
     */
    @Override
    public int updateHealthRecords(HealthRecords healthRecords)
    {
        return healthRecordsMapper.updateHealthRecords(healthRecords);
    }

    /**
     * 批量删除健康数据
     *
     * @param healthIds 需要删除的健康数据主键
     * @return 结果
     */
    @Override
    public int deleteHealthRecordsByHealthIds(String[] healthIds)
    {
        return healthRecordsMapper.deleteHealthRecordsByHealthIds(healthIds);
    }

    /**
     * 删除健康数据信息
     *
     * @param healthId 健康数据主键
     * @return 结果
     */
    @Override
    public int deleteHealthRecordsByHealthId(String healthId)
    {
        return healthRecordsMapper.deleteHealthRecordsByHealthId(healthId);
    }

    @Override
    public List<HealthRecords> selectBloodSugarData(Long userId, String startDate, String endDate) {
        validateUserExistence(userId);
        return healthRecordsMapper.selectBloodSugarData(userId, startDate, endDate);
    }

    @Override
    public List<HealthRecords> selectBloodPressureData(Long userId, String startDate, String endDate) {
        validateUserExistence(userId);
        return healthRecordsMapper.selectBloodPressureData(userId, startDate, endDate);
    }


    private void validateUserExistence(Long userId) {
        if (userId == null) {
            throw new ServiceException("用户ID不能为空");
        }

        SysUser user = sysUserMapper.selectUserById(userId);
        if (user == null) {
            throw new ServiceException("用户ID " + userId + " 不存在");
        }

        if ("1".equals(user.getStatus())) {
            throw new ServiceException("用户 " + user.getUserName() + " 已被禁用");
        }
    }
}
